package com.zy.cat.common.utils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSON;
import com.taobao.api.internal.util.WebUtils;
import com.zy.cat.entity.ec.ECConvertResp;

import lombok.extern.slf4j.Slf4j;

/**
 * @Description: 电商数据API接口提供商（原喵有券开放平台）
 * @author xiake
 * @date 2021-01-05 09:43:44
 */
@Slf4j
public class ECAPI {

	private String apkey = "e780906f-c5eb-dd38-0261-690e1d49bcc3";
	private String pid = "mm_127127112_1520400456_110383000089";
	private String tbname = "zjcails1314";
	private String base_url = "http://api.web.ecapi.cn/";
	private String taoke_url = base_url + "taoke/";

	public ECAPI(String apkey, String pid, String tbname) {
		super();
		this.apkey = apkey;
		this.pid = pid;
		this.tbname = tbname;
	}

	public static void main(String[] args) {
		String itemid = "595613585150", rid = "";
		ECConvertResp result = ECAPI.getInstance(20).getTKConvert(itemid, rid);
		log.info("--->" + JSON.toJSONString(result, true));
	}

	/**
	 * 
	 * @author lipengchao
	 * @apiNote
	 * @date 2021-01-05 10:04:04
	 * @param type 20.淘淘集安卓专用、21.淘淘集IOS专用、
	 * @return
	 */
	public static ECAPI getInstance(int type) {
		String apkey = "e780906f-c5eb-dd38-0261-690e1d49bcc3";
		String pid = "mm_127127112_1520400456_110383000089";
		String tbname = "zjcails1314";
		if (type == 20 || type == 21) {
			apkey = "e780906f-c5eb-dd38-0261-690e1d49bcc3";
			pid = "mm_127127112_1520400456_110383000089";
			tbname = "zjcails1314";
		}
		return new ECAPI(apkey, pid, tbname);
	}

	public ECConvertResp getTKConvert(String content, String rid) {
		ECConvertResp eCConvertResp = null;
		Map<String, String> props = new HashMap<String, String>();

		props.put("apkey", apkey);
		props.put("pid", pid);
		props.put("tbname", tbname);
		props.put("relationid", rid);
		props.put("hasiteminfo", "1");// 是否输出商品信息，双十一期间不建议启用，1=启用，0=不启用
		props.put("shorturl", "1");// 是否需要短链接，1为生成，不填则不生成
		props.put("tpwd", "1");// 是否生成淘口令，1为生成，不填则不生成
//		props.put("extsearch", "1");// 官方查不到券时是否去第三方平台查询，1为启用
		props.put("content", content);

		/**
		 * 本接口参数 content 可传参数类型（请做urlencode编码后传参）：
		 * 
		 * 1.淘宝产品id：比如 578327349261 或直接产品链接 //item.taobao.com/item.htm?id=578327349261
		 * 
		 * 2.店铺推广口令：比如 ￥vJcpcP44KGm￥，店铺转链的推广链接字段为click_url
		 * 
		 * 3.店铺推广链接：https://s.click.taobao.com/I6Q2ytu
		 * 
		 * 4.官方活动口令：3.0￥zujJcNZJjWN￥打кǎI
		 * 
		 * 
		 * 5. 二合一链接：比如 https://uland.taobao.com/coupon/edetail?e=...
		 * 
		 * 6.s.click.taobao.com 长和短链接，比如 https://s.click.taobao.com/CM8azwL
		 * 
		 * 7.天猫的喵口令 如 http://zmnxbc.com/s/ZAa8Z&lt
		 * 
		 * 8.手淘APP的分享链接
		 */

		try {
			String json = WebUtils.doGet(taoke_url + "doItemHighCommissionPromotionLinkByAll", props, "UTF-8");
			if (StringUtils.isNotBlank(json)) {
				log.info("-getTKConvert-->" + json);
				eCConvertResp = JSON.parseObject(json, ECConvertResp.class);
			}
		} catch (IOException e) {
			return new ECConvertResp(-1, e.getLocalizedMessage());
		}
		eCConvertResp = eCConvertResp == null ? new ECConvertResp(404, "转链失败") : eCConvertResp;

		return eCConvertResp;
	}

}
